# 该文件为web主程序
from flask import Flask, request, make_response
import receive
import reply
import traceback
# import xml.etree.ElementTree as et
app = Flask(__name__)

@app.route('/robot',methods=['GET','POST'])
def index():
    try:
		# 专门自定义receive模块来解析从微信服务器获取的数据
        result = receive.parse_xml(request.data)
        if result['type'] == 'text':
		# 专门自定义reply模块来生成xml文本，生成特定内容
        # 还需要向微信服务器提交xml数据，才能将结果发给用户
            response = make_response(reply.send_text(result))
            response.content_type = 'application/xml'
            return response
        elif result['type'] == 'image':
            response = make_response(reply.send_image(result))
            response.content_type = 'application/xml'
            return response
        else:
            print('功能未完善，暂时不处理。')
            response = make_response(reply.noType(result))	
            response.content_type = 'application/xml'
            return response
    except Exception as e:
        traceback.print_exc()
    except TypeError:
        response = make_response(reply.busy(result))
if __name__ == '__main__':
	app.run(host='0.0.0.0',port=80,debug=True)
